/*
 * @Description: enter your description
 * @Autor: 甯匷R锟�?
 * @Date: 2023-09-20 17:47:36
 * @LastEditors: 帅ZR帅
 * @LastEditTime: 2024-03-20 10:57:24
 */
#include <Arduino.h>
#include <BleKeyboard.h>
#include "rtc_wdt.h"
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include "FreeRTOS.h"
#include <HardwareSerial.h>
#include "timers.h"
#include "esp32-hal.h"

#include "TAU1202.h"
#include "RFID.h"
#include "OLED.h"
#include "Bluetooth.h"
#include "IR.h"
#include "CAM.h"
#include "SSEncryptor.h"
#include "Esp_task_wdt.h"
#include "Flash.h"
#include "esp_sleep.h"
#include "esp_efuse.h"
#include "Esp.h"
#include "esp_hmac.h"
int64_t data_test = 123456789;
void (*_resetFunc)(void) = 0; // 系统重启函数
#define BUTTON_PIN_BITMASK 0X4
// RTC_DATA_ATTR
int ETA_MCU = 11; 

//***************************************FreeRTOS************************************************//
int test = 34;
//
TaskHandle_t KEY_Handle;  //
TaskHandle_t GPS_Handle;  //
TaskHandle_t CAM_Handle;  //
TaskHandle_t OLED_Handle; //
TaskHandle_t RFID_Handle;
TaskHandle_t IR_Handle;

//

void KEY_Task(void *args)
{
  while (1)
  {
    Key_Scan();
    vTaskDelay(10 / portTICK_PERIOD_MS);
  }
}

void GPS_Task(void *args)
{
  TAU1202_Init(); 
  vTaskDelay(2000 / portTICK_PERIOD_MS);
  TAU1202_COMMAND(GGA_CLOSE);
  TAU1202_COMMAND(GLL_CLOSE);
  TAU1202_COMMAND(GSA_CLOSE);
  TAU1202_COMMAND(GRS_CLOSE);
  TAU1202_COMMAND(GSV_CLOSE);
  TAU1202_COMMAND(VTG_CLOSE);
  TAU1202_COMMAND(ZDA_CLOSE);
  TAU1202_COMMAND(GST_CLOSE);
  TAU1202_COMMAND(TXT_CLOSE);
  TAU1202_COMMAND(ANT_CLOSE);
  TAU1202_COMMAND(RMC_OPEN);

  while (1)
  {
    TAU1202_READ();
    //     UART_GPS.write('1');
    // printf("1");
    if (GPS_Flag == ID1())
    {

      if (RMC_DATA.location_stauts == 1)
      {

        char str[100];
        char str1[100];
        // sprintf(str, "%.2f,%.6f,%.6f", RMC_DATA.beijing_time, RMC_DATA.latitude, RMC_DATA.longitude);
        sprintf(str, "%.7f", RMC_DATA.latitude);
        sprintf(str1, ", %.7f", RMC_DATA.longitude);
        // if (RMC_DATA.latitude_dir == 1 && RMC_DATA.longitude_dir == 1)
        // {
        //   sprintf(str, "N%d,%.6f", (int)RMC_DATA.latitude / 100, RMC_DATA.latitude - (int)RMC_DATA.latitude / 100 * 100);
        //   sprintf(str1, "  E%d,%.6f", (int)RMC_DATA.longitude / 100, RMC_DATA.longitude - (int)RMC_DATA.longitude / 100 * 100);
        // }
        // if (RMC_DATA.latitude_dir == 1 && RMC_DATA.longitude_dir == 2)
        // {
        //   sprintf(str, "N%d,%.6f", (int)RMC_DATA.latitude / 100, RMC_DATA.latitude - (int)RMC_DATA.latitude / 100 * 100);
        //   sprintf(str1, "  W%d,%.6f", (int)RMC_DATA.longitude / 100, RMC_DATA.longitude - (int)RMC_DATA.longitude / 100 * 100);
        // }
        // if (RMC_DATA.latitude_dir == 2 && RMC_DATA.longitude_dir == 1)
        // {
        //   sprintf(str, "S%d,%.6f", (int)RMC_DATA.latitude / 100, RMC_DATA.latitude - (int)RMC_DATA.latitude / 100 * 100);
        //   sprintf(str1, "  E%d,%.6f", (int)RMC_DATA.longitude / 100, RMC_DATA.longitude - (int)RMC_DATA.longitude / 100 * 100);
        // }
        // if (RMC_DATA.latitude_dir == 2 && RMC_DATA.longitude_dir == 2)
        // {
        //   sprintf(str, "S%d,%.6f", (int)RMC_DATA.latitude / 100, RMC_DATA.latitude - (int)RMC_DATA.latitude / 100 * 100);
        //   sprintf(str1, "  W%d,%.6f", (int)RMC_DATA.longitude / 100, RMC_DATA.longitude - (int)RMC_DATA.longitude / 100 * 100);
        // }
        char *newValue = str;
        // pCharacteristic->setValue(newValue);
        // pCharacteristic->notify();
        bleKeyboard.print(newValue);
        vTaskDelay(100 / portTICK_PERIOD_MS);
        newValue = str1;
        bleKeyboard.print(newValue);
        GPS_Flag = 0;
      }
      else
      {
        char str[100];
        sprintf(str, "NOGPS");
        char *newValue = str;
        bleKeyboard.print(newValue);
        vTaskDelay(100 / portTICK_PERIOD_MS);
        GPS_Flag = 0;
      }
    }
  }
}
void OLED_Task(void *args)
{
  OLED_Init();
  u8g2.setFont(u8g2_font_unifont_t_chinese3);

  while (1)
  {
    Menu_Function();
    vTaskDelay(50 / portTICK_PERIOD_MS);
  }
}

void CAM_Task(void *args)
{
  CAM_Init();
  CAM_Write();

  UART_CAM.begin(1200, SERIAL_8E1, 5 * ID1(), -ID1());
  pinMode(CAM_TRIG, OUTPUT); // OLED 鐢垫簮寮曡剼鍒濆锟�??
  pinMode(MCU_CAM, OUTPUT);  // OLED 鐢垫簮寮曡剼鍒濆锟�??
  digitalWrite(CAM_TRIG, HIGH);
  while (1)
  {
    digitalWrite(MCU_CAM, HIGH);
    if (scan_mode == 2 && CAM_Flag >= 1 * ID1())
    {
      vTaskDelay(500 / portTICK_PERIOD_MS);

      if (UART_CAM.available())
      {
        CAM_Scan();
      }
      else
      {
        vTaskDelay(20 / portTICK_PERIOD_MS);
      }
      if (CAM_Flag > 0)
      {
        digitalWrite(MCU_CAM, HIGH);
        for (int i = ID0(); i < 9 * ID1(); i++)
        {
          UART_CAM.write(CAM_Scan_COM[i]);
        }
      }

    }
    else
    {

      vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
  }
}

void RFID_Task(void *args)
{
  RFID_Init();
  digitalWrite(MCU_RFID_EN, LOW);
  int RFID_Time = 0;
  while (1)
  {
    if (scan_mode == 1)
    {
      switch (rfid_list[0].data)
      {
      case 0:
        if (RFID_Flag > 0)
        {
          digitalWrite(14, HIGH);
        }
        else
        {
          digitalWrite(14, LOW);
        }
        break;
      case 1:
        digitalWrite(14, HIGH);
        break;
      case 2:
        digitalWrite(14, LOW);
        break;
      }
    }
    if (scan_mode == 1 && RFID_Flag > 0)
    {
      digitalWrite(MCU_RFID_EN, HIGH);
      RFID_COMMAND(RFID_scan);
      RFID_READ();
      if (RFID_Time++ > 600)
      {
        memset(scan_data_1, 0, sizeof(scan_data_1));
        memset(scan_data_2, 0, sizeof(scan_data_2));
        RFID_Time = 0;
        if (rfid_list[1].data == 1)
        {
          if (RFID_Flag > 0)
          {
            RFID_Flag--;
          }
          digitalWrite(MCU_BEEP, HIGH);
          vTaskDelay(100 / portTICK_PERIOD_MS);
          digitalWrite(MCU_BEEP, LOW);
        }
        else
        {
          RFID_Flag = 0;
        }
      }
    }
    else
    {
      digitalWrite(MCU_RFID_EN, LOW);
      vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
  }
}

void IR_Task(void *args)
{
  IR_Init();
  digitalWrite(14, LOW);
  while (1)
  {
    if (scan_mode == 0)
    {
      switch (ir_list[0].data)
      {
      case 0:
        if (IR_Flag > 0)
        {
          digitalWrite(14, HIGH);
        }
        else
        {
          digitalWrite(14, LOW);
        }
        break;
      case 1:
        digitalWrite(14, HIGH);
        break;
      case 2:
        digitalWrite(14, LOW);
        break;
      }
    }
    if (scan_mode == 0 && IR_Flag > 0)
    {
      IR_698_Scan();
      IR_645_Scan();
      if (ir_list[1].data == 1)
      {
        if (IR_Flag > 0)
        {
          IR_Flag--;
        }
        digitalWrite(MCU_BEEP, HIGH);
        vTaskDelay(100 / portTICK_PERIOD_MS);
        digitalWrite(MCU_BEEP, LOW);
      }
      else
      {
        IR_Flag = 0;
      }
    }
    else
    {
      vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
  }
}
RTC_DATA_ATTR int bootCount = 0;
void setup()
{
  bleKeyboard.begin();

  while (1)
  {
    ++bootCount;
    bleKeyboard.print(bootCount);
    printf("shut_down%d", bootCount);
  }
  if (ID0() != 0 || ID1() != 1 || ID0() + ID1() != 1)
  {
    _resetFunc();
  }

  xTaskCreatePinnedToCore(KEY_Task, "KEY_Task", 20480, NULL, 7, &KEY_Handle, 1);
  xTaskCreatePinnedToCore(OLED_Task, "OLED_Task", 20480, NULL, 6, &OLED_Handle, 1);
  xTaskCreatePinnedToCore(CAM_Task, "CAM_Task", 20480, NULL, 3, &CAM_Handle, ID1());

  xTaskCreatePinnedToCore(RFID_Task, "RFID_Task", 20480, NULL, 3, &RFID_Handle, 1);

  xTaskCreatePinnedToCore(GPS_Task, "GPS_Task", 20480, NULL, 3, &GPS_Handle, 0);
  xTaskCreatePinnedToCore(IR_Task, "IR_Task", 20480 * 4, NULL, 6, &IR_Handle, 1);
}
int time_1 = 0;
int time_2 = 0;

char data_send[1000];
void loop()
{
  data++;
  ++bootCount;
  bleKeyboard.print(bootCount);
  printf("shut_down%d", bootCount);
  printf("Current time: %ds,%d,%d\n", data, ID0(), ID1());
  vTaskDelay(1000 / portTICK_PERIOD_MS);
}